﻿#include "confirmdevice.h"
#include <QSerialPortInfo>
#include <QDebug>
#include <thread>
#include <QMessageBox>
#include <QDateTime>
#include <functional>
#include <Windows.h>
#include "Template/singleton.h"
#include "CTDWidget/dockwidgetctd.h"

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif


ConfirmDevice::ConfirmDevice(QObject *parent)
    : QObject(parent)
    ,m_timer(new QTimer(this))
    ,adapterDeviceTimer(new QTimer(this))
    ,dockWidgetTsc(DockWidgetCTDInstance)
{
    initSerialPort();
}

void ConfirmDevice::initSerialPortBack(QString& portName)
{

    serial.setPortName(portName);
    SetSerialPortParam(&serial, QSerialPort::Baud19200, QByteArray::fromHex("EB900701000000000000000000000000000000"));

    // 清空缓存,防止叫不到
    initReadAll.clear();
}



// 问题分为两类
// 1: 软件打开的时候，有多少台设备显示多少台设备
// 1.1: 先定义一个串口号,用QSerialPortInfo::availablePorts()，给个设备名字号，试;
// 1.2: 试出来之后，将该串口号给当前正确的串口
// 
// 
// 2: 热插拔的时候，插入一台设备，显示一台设备; 拔出一台设备,去除一台设备
// 2.1: 用正确的串口名匹配 热插拔检测的串口名

void ConfirmDevice::initSerialPort()
{
    connect(&serial, &QSerialPort::readyRead, this, &ConfirmDevice::readAdapterResponse);
    infos = QSerialPortInfo::availablePorts();
    adapterDeviceTimer.start(1500);
    connect(&adapterDeviceTimer, &QTimer::timeout, [&]() {
        if (infos.size() > 0)
        {
            serial.setPortName(infos.at(g_com_cont).portName());
        }
        SetSerialPortParam(&serial, QSerialPort::Baud19200, QByteArray::fromHex("EB900701000000000000000000000000000000"));
        // SetSerialPortParam(&serial, QSerialPort::Baud19200, "DHI\r\n");  //查设备
        // QTimer::singleShot(200, this, [=]() {SetSerialPortParam(&serial, QSerialPort::Baud19200, "DHI\r\n"); });
        g_com_cont++;
        if (infos.size() == g_com_cont) {
            adapterDeviceTimer.stop();
            // 断开信号和槽
            // disconnect(&serial, &QSerialPort::readyRead, this, &ConfirmDevice::readAdapterResponse);
        }
        });
}

void ConfirmDevice::confirmCommandTCD()
{
    SetSerialPortParam(&serial, QSerialPort::Baud19200, "DHI\r\n");  //查设备
    QTimer::singleShot(200, this, [=]() {SetSerialPortParam(&serial, QSerialPort::Baud19200, "DHI\r\n"); });
}



// 这个函数主要区分是哪个类型的设备，将函数名赋值给该串口号
void ConfirmDevice::readAdapterResponse()   
{
    // 和上面互斥
    QByteArray readOnce = serial.readAll().toHex();
    initReadAll.append(readOnce);
    // 说明是TD-SC
    if (initReadAll.startsWith("5444") && initReadAll.endsWith("474947"))
    {  
        initReadAll.clear();
        // 将串口名字给 serialTD
        serial_TD.setPortName(serial.portName());
        serial.close();
        connect(&serial_TD, &QSerialPort::readyRead, this, &ConfirmDevice::readResponse_TD);
        SetSerialPortParam(&serial_TD, QSerialPort::Baud19200, QByteArray::fromHex("EB900701000000000000000000000000000000"));    // 474947

        // 清缓存防止热插拔识别不到
        response_TD.clear();
    }
    // 说明是T-SC
    else if (initReadAll.contains("48454c4c4f")|| (initReadAll.startsWith("5443") && initReadAll.endsWith("474946")))
    {
        qDebug() << "333333333333"<< initReadAll;
        initReadAll.clear();
        // 更改串口名字
        serial_T.setPortName(serial.portName());
        serial.close();
        connect(&serial_T, &QSerialPort::readyRead, this, &ConfirmDevice::readResponse_T);

        // 进到这里说明是 T 的设备，添加右侧导航栏
        Message_t stuMsg;
        stuMsg.deviceType = "5443";
        stuMsg.arrData = initReadAll;
        emit sendTypeData(stuMsg);

		// 两种情况，可能会出现 hello 或者 5443   
		SetSerialPortParam(&serial_T, QSerialPort::Baud19200, QByteArray::fromHex("EB900800000000000000000000000000000000")); //返回474256
    }     
    // initReadAll = "446576696365547970653d4354442d30363138494d0d0a53657269616c4e756d6265723d3130303232370d0a4669726d776172655665723d312e302e30610d0a4669726d7761726554696d653d32312d30372d30310d0a4d656d6f727943617061636974793d3132384d420d0a496e746572666163653d56310d0a537570706c793d372e3038760d0a3e";
    else if (initReadAll.startsWith("446576696365547970653d4354442d30363138")|| initReadAll.startsWith("2450524d53"))    // CTD-0618R  $PRMSE
    {
        initReadAll.clear();
        serial.close();
        // 进到这里说明是 CTD_IM 的设备，添加右侧导航栏
        Message_t stuMsg;
        stuMsg.deviceType = "435444";  // CTD_IM
        stuMsg.arrData = initReadAll;
        emit sendTypeData(stuMsg);

        //serial_CTD.setPortName(serial.portName());
        dockWidgetTsc->sendBaudAndPortName(QSerialPort::Baud19200, serial.portName());

        // 说明是 CTD_IM 正在采样
        if (initReadAll.startsWith("2450524d53"))
        {
            // 稍后处理
        }
    }
    //initReadAll.clear();
}

void ConfirmDevice::sendData_back()
{
    QByteArray sendHexTD = QByteArray::fromHex("EB900701000000000000000000000000000000");  // TD
    QByteArray sendHexT = QByteArray::fromHex("EB900700000000000000000000000000000000");   // T
    // QByteArray sendIM = "DHI";
    // QByteArray sendIM = QByteArray::fromHex("DHI");
    QByteArray sendIM = QByteArray::fromHex(QString::number('DHI',16).toUtf8());


    if(g_com_cont<m_serialList.size()*4*3)  // g_com_cont < 12
    {
        // serial_TD.setPortName(m_serialList.at((int)g_com_cont/(4*3)));
        // serial.setPortName(m_serialList.at((int)g_com_cont/(4*3)));
        switch(g_com_cont%(4*3))
        {
        case 0:
            SetSerialPortParam(&serial,QSerialPort::Baud19200,sendHexTD);
            break;
        case 1:
            SetSerialPortParam(&serial_T,QSerialPort::Baud19200,sendHexT);
            break;
        case 2:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud19200,sendIM);
            break;
        case 3:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud9600,sendHexTD);
            break;
        case 4:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud9600,sendHexT);
            break;
        case 5:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud9600,sendIM);
            break;
        case 6:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud38400,sendHexTD);
            break;
        case 7:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud38400,sendHexT);
            break;
        case 8:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud38400,sendIM);
            break;
        case 9:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud115200,sendHexTD);
            break;
        case 10:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud115200,sendHexT);
            break;
        case 11:
            SetSerialPortParam(&serial_TD,QSerialPort::Baud115200,sendIM);
            break;
        }
        g_com_cont++;

    }
}






bool ConfirmDevice::SetSerialPortParam(QSerialPort *serial, int Baudrate,QByteArray serialCommand)
{
    serial->setReadBufferSize(0);
    serial->setBaudRate(Baudrate);
    serial->setDataBits(QSerialPort::Data8);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setParity(QSerialPort::NoParity);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->close();
    if(!serial->open(QIODevice::ReadWrite))
    {
        qDebug()<<"not is open";
        //QMessageBox::information(NULL, "TD-SC", "端口号打开失败",QMessageBox::Ok);
        return false;
    }
    serial->write(serialCommand);
    qDebug() << serialCommand;
    return true;
}


void ConfirmDevice::setTemperatureCoefficientABCD(QByteArray dataAB,QByteArray dataCD)
{
    serial_TD.write(QByteArray::fromHex("EB9011"+dataAB));  // 534341
    qDebug() << "EB9011" + dataAB;
}

void ConfirmDevice::reciveTemperatureCD(QByteArray dataCD)
{

    serial_TD.write(QByteArray::fromHex("EB9012"+dataCD));  // 534343
    qDebug() << "EB9012" + dataCD;
}

void ConfirmDevice::recivePressureCoefficientAB(QByteArray dataAB)
{
    serial_TD.write(QByteArray::fromHex("EB9018"+dataAB));
    qDebug() << "EB9018" + dataAB;
}

void ConfirmDevice::recive_Pressure_CoefficientCD(QByteArray dataCD)
{
    serial_TD.write(QByteArray::fromHex("EB9019"+dataCD));
    qDebug() << "EB9019" + dataCD;
}

void ConfirmDevice::recive_Calibration_Time(QByteArray dataTime)
{
    serial_TD.write(QByteArray::fromHex("EB901602" + dataTime + "000000000000000000")); // 534654
    qDebug() << "EB901602" + dataTime + "000000000000000000";
}

void ConfirmDevice::send_Calibration_Coefficien()
{
    serial_TD.write(QByteArray::fromHex("EB900600000000000000000000000000000000"));
    qDebug() << "EB900600000000000000000000000000000000";
}

void ConfirmDevice::recive_Start_Sample(QByteArray data)
{
    serial_TD.write(QByteArray::fromHex(data));
    qDebug() << data;
}

void ConfirmDevice::recive_Stop_Sample()
{
    serial_TD.write(QByteArray::fromHex("EB900400000000000000000000000000000000"));  // 535450
    qDebug() << "EB900400000000000000000000000000000000";
}

void ConfirmDevice::getRotationDeviceTime()
{
    // 定时器 m_Heart_timer 500 毫秒获取一次状态
    serial_TD.write(QByteArray::fromHex("EB900100000000000000000000000000000000"));  // 475444
    qDebug() << "EB900100000000000000000000000000000000";
}

void ConfirmDevice::get_Sample_Status()
{
    // 定时器 m_Heart_timer 500 毫秒获取一次状态
    serial_TD.write(QByteArray::fromHex("EB900700000000000000000000000000000000"));  //474946
	//qDebug() << "EB900700000000000000000000000000000000";
}

void ConfirmDevice::send_Read_Sample_Config()
{
    serial_TD.write(QByteArray::fromHex("EB901000000000000000000000000000000000")); // 475449
    qDebug() << "EB901000000000000000000000000000000000";
}

void ConfirmDevice::send_Read_Flash_Using_DownLoad()
{
    serial_TD.setBaudRate(QSerialPort::Baud115200);
    serial_TD.write(QByteArray::fromHex("EB900500000000000000000000000000000000")); // FCFCFC XXXXX… FEFEFE
    qDebug() << "EB900500000000000000000000000000000000";
    emit sendStopTimer();


}

void ConfirmDevice::send_Once_Sample()
{
    serial_TD.write(QByteArray::fromHex("EB901400000000000000000000000000000000")); // 475254
    qDebug() << "EB901400000000000000000000000000000000";
}

void ConfirmDevice::recvMessageFromDeviceConfigTSC(ModetoConfig mode)
{
	switch (mode.DateType)
	{
	case 1:
        qDebug() << "0000A " << "EB9011" + mode.arrDataA.toHex();   // 534341
        serial_T.write(QByteArray::fromHex("EB9011" + mode.arrDataA.toHex()));
        arrTemperatureCoefficientBTSC = mode.arrDataB;
		break;
    case 2:
        this->arrLED = mode.arrDataB;
        this->start_Sample = mode.arrDataA;
        serial_T.write(QByteArray::fromHex("EB901500000000000000000000000000000000"));  // 464D54
        break;
	default:
		break;
	}
    
}


void ConfirmDevice::readResponse_TD()
{
    QByteArray ReadAll = serial_TD.readAll().toHex();
    if(ReadAll.contains("01010154494d")|| ReadAll.startsWith("fcfc")|| ReadAll.startsWith("010101535450"))    // 数据不对,易出问题，从这里找原因
    {
        //QByteArray str = "01010154494d2305091326245d69d7022f0c5d520a022ef55d40da022f075d184d022f035d3705022f645d8c9f022ed25e04b3022eb55e3f86022ee75e3a7e022ed85e461b022eed5e654d022ee15ea9b3022eae5edae1022ecd5eafe7022ef15e8a08022ee15e6903022f035e9179022eca5e9335022ee55e70b1022ec55e5310022ee65e4935022efc5e341a022ece5e21a3022ec55e19d6022ef05e06f7022ecb5df430022ee15de6f9022edb5dd7ae022ec15dcb8d022ee85db71a022ecd5d9a64022eef5d935b022f0e5d90c6022f0f5d8cc9022edd5d7cb7022f0e5d76b8022f1a5d71a1022f00010101535450230509133005";
        emit send_Flash_Info(ReadAll);
        ReadAll.clear();
    }

    response_TD.append(ReadAll);
    Message_t  stuMsg;
    // TD 的设备
    if(response_TD.startsWith("5444") && response_TD.contains("474947"))
    {
        stuMsg.deviceType="5444";
        stuMsg.endWithType=474947;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);
        response_TD.clear();
        qDebug()<<"找到设备，停止定时器m_timer";
        m_timer.stop();
        // 电池
        serial_TD.write(QByteArray::fromHex("EB900800000000000000000000000000000000")); //474256
        qDebug() << "EB900800000000000000000000000000000000";
    }


    // TD 的电池信息设备(前提已经是TD的设备了)
    else if(response_TD.contains("474256"))
    {
        qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=474256;
        stuMsg.arrData= response_TD;

        emit sendTypeData(stuMsg);
        response_TD.clear();
        // 采样配置
        send_Read_Sample_Config();  // 475449
    }

    // TD 获取配置信息
    else if(response_TD.contains("475449"))
    {
        qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=475449;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);

        response_TD.clear();
        // 读温度系数 ABCD
        serial_TD.write(QByteArray::fromHex("EB900600000000000000000000000000000000"));  // 474354
        qDebug() << "EB900600000000000000000000000000000000";
    }

    else if(response_TD.contains("474354"))
    {
        qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=474354;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);

        response_TD.clear();
        // 读压力系数 ABCD
        serial_TD.write(QByteArray::fromHex("EB900601000000000000000000000000000000"));  // 474350
        qDebug() << "EB900601000000000000000000000000000000";
    }

    else if (response_TD.contains("474350"))
    {
        qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType = "5444";
        stuMsg.endWithType = 474350;
        stuMsg.arrData = response_TD;
        emit sendTypeData(stuMsg);
        response_TD.clear();
        // 到这里没有写,m_timer连锁反应到这里停掉
    }

    // 定时器500 毫秒 读 TD 设备状态
    else if (response_TD.contains("474946"))
    {
        qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType = "5444";
        stuMsg.endWithType = 474946;
        stuMsg.arrData = response_TD;
        emit sendTypeData(stuMsg);
        response_TD.clear();
        //getRotationDeviceTime();  // 475444
    }

    // 定时器500 毫秒 读 TD 设备时间
    else if(response_TD.contains("475444"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=475444;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);
        response_TD.clear();
        // 定时器 m_Heart_timer 在这里一直500毫秒循环执行
    }


    // TD 的设置本地时间
    else if(response_TD.contains("535444"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=535444;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);
        response_TD.clear();
        // 同步时间完
    }

    // sendTemperatureAB  EB9011
    else if(response_TD.contains("534341"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=534341;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);

        response_TD.clear();
    }
    // sendTemperatureCD
    else if(response_TD.contains("534343"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=534343;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);

        response_TD.clear();
    }

    // sendPresureAB
    else if(response_TD.contains("534342"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=534342;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);

        response_TD.clear();
    }

    // sendPresureCD
    else if(response_TD.contains("534344"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=534344;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);

        response_TD.clear();
    }

    // 校准时间
    else if(response_TD.contains("534654"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=534654;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);

        response_TD.clear();
    }

    // 响应 清空flash 处理
    else if(response_TD.contains("464d54"))
    {
        stuMsg.deviceType = "5444";
        stuMsg.endWithType = 46454;
        stuMsg.arrData = response_TD;
        emit sendTypeData(stuMsg);
        response_TD.clear();
    }

    // 启用LED之后，下发采样指令
    else if(response_TD.contains("01534643")|| response_TD.contains("00534643"))
    {
        qDebug() << "response_TD=" << response_TD;
        response_TD.clear();
        // 下发采样指令
        stuMsg.deviceType = "5444";
        stuMsg.endWithType = 737461;    // 737461 是 STA 的意思
        stuMsg.arrData = response_TD;
        emit sendTypeData(stuMsg);
    }

    // 开始采样成功设置 下位机返回两次 535350
    else if(response_TD.endsWith("535350"))
    {
        qDebug() << "response_TD=" << response_TD;
        //设置状态
	    emit send_Set_Btn_Status(response_TD);
        response_TD.clear();   
    }

    // 停止采样指令响应
    else if(response_TD.endsWith("535450"))
    {
    qDebug() << "response_TD=" << response_TD;
    response_TD.clear();
        //设置状态
        emit send_Stop_After_Change_Status();
    }

    // 每隔一秒采样
    else if(response_TD.endsWith("475254"))
    {
    qDebug() << "response_TD=" << response_TD;
        stuMsg.deviceType="5444";
        stuMsg.endWithType=475254;
        stuMsg.arrData= response_TD;
        emit sendTypeData(stuMsg);
        response_TD.clear();
    }

    //48454c4c4f hello  4552524f52 error
    else if(response_TD.contains("4552524f52"))
    {
    qDebug() << "response_TD=" << response_TD;
    response_TD.clear();
        if(!isCrashFlag){
            isCrashFlag=true;
            QMessageBox::information(NULL, "TD-SC", "下位机错误,需要重新插拔",QMessageBox::Ok);
            return;
        }
    }

}



void ConfirmDevice::readResponse_T()
{
    Message_t stuMsg;
    stuMsg.deviceType = "5443";
    QByteArray readAll = serial_T.readAll().toHex();
    responseTSC.append(readAll);
    if (responseTSC.size() > 1030 && (responseTSC.contains("01010154494d") || responseTSC.startsWith("fcfc") || responseTSC.startsWith("010101535450")|| responseTSC.endsWith("fefefe")))    // 数据不对,易出问题，从这里找原因
    {
        emit sendFlashDataFromTSC(responseTSC);
        responseTSC.clear();
    }
    // 说明重新插入设备
	else if (responseTSC.startsWith("48454c4c4f"))
	{
		responseTSC.remove(0, 10);
        // 这里说明 T-SC 使用下面那个口更换设备
        SetSerialPortParam(&serial_T, QSerialPort::Baud19200, QByteArray::fromHex("EB900800000000000000000000000000000000")); // 474256
	}
    // 电池返回的信息
	else if (responseTSC.endsWith("474256"))
	{
		stuMsg.endWithType = 474256;
		stuMsg.arrData = responseTSC;
		emit sendTypeTSC(stuMsg);
		responseTSC.clear();
		// 读 T 配置
		serial_T.write(QByteArray::fromHex("EB901000000000000000000000000000000000"));  // 475449
        QTimer::singleShot(200, this, [=]() {serial_T.write(QByteArray::fromHex("EB901000000000000000000000000000000000")); });
	}

	else if (responseTSC.endsWith("475449"))
	{
		stuMsg.endWithType = 475449;
		stuMsg.arrData = responseTSC;
		emit sendTypeTSC(stuMsg);
		responseTSC.clear();
        // 读系数
		serial_T.write(QByteArray::fromHex("EB900600000000000000000000000000000000"));  // 47434C
	}
	// 读温度系数返回的
	else if (responseTSC.endsWith("47434c"))
	{
		if (responseTSC.contains("48454c4c4f"))
		{
			responseTSC.remove(0, 10);
		}
		stuMsg.endWithType = 47434;
		stuMsg.arrData = responseTSC;
		emit sendTypeTSC(stuMsg);
		responseTSC.clear();
	}

	// 发送完温度系数 AB 返回的
	else if (responseTSC.endsWith("534341"))   // 534341
	{
		if (responseTSC.contains("48454c4c4f"))
		{
			responseTSC.remove(0, 10);
		}
		responseTSC.clear();
		serial_T.write(QByteArray::fromHex("EB9012" + arrTemperatureCoefficientBTSC.toHex()));
	}
    else if (responseTSC.contains("534343"))  // 534343
    {
        // 对 EB9012 的返回值做出清空，以免出现脏数据
        responseTSC.clear();
        QByteArray arrCurTime = QDateTime::currentDateTime().toString("yyMMddhhmmss").toUtf8();
        serial_T.write(QByteArray::fromHex("EB901602"+ arrCurTime + "000000000000000000"));  // 534354
    }
    // 校准时间返回值
    else if(responseTSC.contains("534354"))
    {
        stuMsg.endWithType = 534354;
        stuMsg.arrData = responseTSC;
        emit sendTypeTSC(stuMsg);
        responseTSC.clear();
    }
    // 读硬件信息
	//else if (responseTSC.startsWith("544331") && responseTSC.endsWith("474946"))
	else if (responseTSC.contains("474946"))
	{
		stuMsg.arrData = responseTSC;
		stuMsg.endWithType = 474946;
		emit sendTypeTSC(stuMsg);
		responseTSC.clear();
	}

    // 同步时间
    else if(responseTSC.endsWith("535444"))
    {
        // 发送 deviceConfigTab 类 更新状态和时间
        stuMsg.arrData = responseTSC;
        stuMsg.endWithType = 535444;
        emit sendTypeTSC(stuMsg);
        responseTSC.clear();
    }
    // 清空内存返回的数据
    else if(responseTSC.endsWith("464d54"))
    {
        stuMsg.arrData = responseTSC;
        stuMsg.endWithType = 46454;
        emit sendTypeTSC(stuMsg);
        responseTSC.clear();
        // serial_T.write(QByteArray::fromHex("EB9016" +arrLED +"000000000000000000000000000000"));
    }
    //
    else if (responseTSC.contains("534643"))
    {        
        stuMsg.endWithType = 737461;    // 737461 是 STA 的意思
        stuMsg.arrData = responseTSC;
        emit sendTypeTSC(stuMsg);
        responseTSC.clear();
    }
    else if(responseTSC.contains("535350"))
    {
        stuMsg.arrData = responseTSC;
        stuMsg.endWithType = 535350;
        emit sendTypeTSC(stuMsg);
        responseTSC.clear();
    }
    // 535450 停止采样,为了避免和下载冲突 使用等号
	else if (responseTSC == "535450")
	{
	    responseTSC.clear();
	}
    // 打点采样
	else if (responseTSC.contains("475254"))
	{
	    emit sendSampleRotateTSC(responseTSC);
	    responseTSC.clear();
	}
}



void ConfirmDevice::sendComRemove(QString& portName)
{
    qDebug() << "portName:" << portName;
    qDebug() << "serial_T.portName()" << serial_T.portName();
    qDebug() << "serial_TD.portName()" << serial_TD.portName();

    int serialNumber = QSerialPortInfo::availablePorts().size();
    if (serialNumber == 0)
    {
        // 发送单T串口拔出指令，做了三件事; 1:tab界面变灰掉,2:下面打点变灰掉，3:item 删除;对应三个槽函数
        emit sendComRemove_TSC_Signal(portName);
    }
    // 大于 1,表示留有最后一个,不能全部隐藏
    if (serialNumber >= 1)
    {
        if (portName == serial_T.portName())
        {
            // 发送单T串口拔出指令，做了三件事; 1:tab界面变灰掉,2:下面打点变灰掉，3:item 删除;对应三个槽函数
            emit sendComRemove_TSC_Signal(portName);
        }
        else if (portName == serial_TD.portName())
        {
            emit sendComRemove_TDSC_Signal(portName);
        }
    }
    
}
